//
//  CYEmoticonKeyboardVC.m
//  LingLingBang
//
//  Created by 郝俊聪 on 15/12/28.
//  Copyright © 2015年 zhaohongling. All rights reserved.
//

#import "CYEmoticonKeyboardView.h"
#import "CYEmoticonCell.h"
#import "CYEmoticon.h"

@interface CYEmoticonKeyboardView ()<UICollectionViewDataSource,UICollectionViewDelegate>
//collectionView
@property (nonatomic , strong) UICollectionView *emoticonView;
//表情数组
@property (nonatomic, strong) NSArray *emoticons;

@end

@implementation CYEmoticonKeyboardView

////表情键盘单例
//+ (instancetype)shareEmoticonKeyBoardView
//{
//    static CYEmoticonKeyboardView *emoticonKeyboardView;
//    static dispatch_once_t onceToken;
//    dispatch_once(&onceToken, ^{
//        emoticonKeyboardView = [[CYEmoticonKeyboardView alloc] initWithFrame:CGRectMake(0, 0, IPHONE_WIDTH, 200)];
//    });
//    return emoticonKeyboardView;
//}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setUI];
        //获取数据
        self.emoticons = [CYEmoticon allEmoticonsFromPlist];
    }
    return self;
}


//设置UI
- (void)setUI
{
    self.emoticonView.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
    //注册cell
    [self.emoticonView registerClass:[CYEmoticonCell class] forCellWithReuseIdentifier:@"CYEmoticonCell"];
    self.emoticonView.delegate = self;
    self.emoticonView.dataSource = self;
}


//懒加载
- (UICollectionView *)emoticonView
{
    if (!_emoticonView) {
        //创建布局
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        CGFloat w = KScreenWidth/7;
        layout.itemSize = CGSizeMake(w, w);
        layout.minimumInteritemSpacing = 0;
        layout.minimumLineSpacing = 0;
        layout.sectionInset = UIEdgeInsetsMake(4, 0, 0, 0);
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _emoticonView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _emoticonView.pagingEnabled = true;
        _emoticonView.backgroundColor = [UIColor whiteColor];
        _emoticonView.delegate = self;
        _emoticonView.dataSource = self;
        [self addSubview:_emoticonView];
    }
    return _emoticonView;
}

- (void)setEmoticons:(NSArray *)emoticons
{
    _emoticons = emoticons;
    [_emoticonView reloadData];
}


#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.emoticons.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CYEmoticonCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CYEmoticonCell" forIndexPath:indexPath];
    cell.emoticon = self.emoticons[indexPath.item];
    return cell;
}

#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    CYEmoticonCell *cell = (CYEmoticonCell *)[collectionView cellForItemAtIndexPath:indexPath];
    if (self.InsertEmoticon) {
        //执行block
        self.InsertEmoticon(cell);
    }
}

@end
